<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class Fungsi {

    public function SetVariableToMultiFormVariableSupport($pMultiFormVariableName, $pValue, $pInc = false, $pMultiForm = true) {
        $tValidation = new InputValidation();
        $tValidation->SetInput($_POST[fMultiFormCount]);
        $tValidation->SetPattern(pUnsignedInt);
        $mMultiFormCount = $tValidation->CheckValidation();

        //JIKA DATATYPE VALUE = ARRAY, MAKA NILAI BERAGAM, Jika single variable nilai sama semua
        if ($pMultiForm) {
            if (is_array($pValue)) {
                for ($i = 1; $i <= ($mMultiFormCount ? $mMultiFormCount : MAX_MULTIPLE_INPUT); $i++) {
                    $_POST[$pMultiFormVariableName . '_' . $i] = $pValue[$i - 1];
                }
            } else {
                for ($i = 1; $i <= ($mMultiFormCount ? $mMultiFormCount : MAX_MULTIPLE_INPUT); $i++) {
                    $_POST[$pMultiFormVariableName . '_' . $i] = $pValue;
                    if ($pInc)
                        $pValue++;
                }
            }
        }
        else {
            $_POST[$pMultiFormVariableName] = $pValue;
        }
    }

    public function ConvertEmptyToNull($pInputName, $pMultiForm = true) {
        $tValidation = new InputValidation();
        $tValidation->SetInput($_POST[fMultiFormCount]);
        $tValidation->SetPattern(pUnsignedInt);
        $mMultiFormCount = $tValidation->CheckValidation();

        if ($pMultiForm) {
            for ($i = 1; $i <= ($mMultiFormCount ? $mMultiFormCount : MAX_MULTIPLE_INPUT); $i++) {
                if (empty($_POST[$pInputName . '_' . $i]))
                    $_POST[$pInputName . '_' . $i] = NULL;
            }
        } else {
            if (empty($_POST[$pInputName]))
                $_POST[$pInputName] = NULL;
        }
    }

    public function ConvertDateToDBFormat($pInputName, $pFormat, $pIsMultiForm = false) {
        $mSeparatorLib = "/-";
        $tTargetFormat = "YYYY-MM-DD";
        $pFormat = strtoupper($pFormat);
        $pCurrentInput = $_POST[$pInputName];

        //CHECK: Date Separator yang digunakan
        $tFound = false;
        while ($i < strlen($pFormat) && !$tFound) {
            $temp = $pFormat[$i];
            if (is_int(strpos($mSeparatorLib, $temp))) {
                $mSeparator = substr($pFormat, $i, 1);
                $tFound = true;
            }
            $i++;
        }

        //CHECKING FORMAT
        $tResFormat = explode($mSeparator, $pFormat);
        $tTargetFormat = explode("-", $tTargetFormat);

        //Pisah setiap Tanggal/Bulan/Tahun sesuai dengan date separator yang didapat
        if ($pIsMultiForm) {
            for ($j = 1; $j <= MAX_MULTIPLE_INPUT; $j++) {
                if (!empty($_POST[$pInputName . '_' . $j])) {
                    $tDate = explode($mSeparator, $_POST[$pInputName . '_' . $j]);

                    //LAKUKAN KONVERSI, Timpa variabel global yang asli
                    //PENCOCOKAN URUTAN (SELALU 3 urutan yaitu D, M dan Y)
                    for ($z = 0; $z < 3; $z++) {
                        if ($tTargetFormat[$z] != $tResFormat[0]) {
                            $tTarget = substr($tTargetFormat[$z], 0, 1);

                            $x = 0;
                            $tFoundOn = 0;
                            while ($tResFormat[$x]) {
                                $temp = substr($tResFormat[$x], 0, 1);
                                //echo $tResFormat[$x].'|: '.$temp.' | '.$tTarget.'<br>';  
                                if ($tTarget == $temp) {
                                    $tFoundOn = $x;
                                    break;
                                }
                                $x++;
                            }
                            //echo '<br>'.$z.' |: FOUND: '.$tFoundOn.' | '.$tTargetFormat[$z].' | '.$tResFormat[0].'<br>';

                            if (strlen($tResFormat[$tFoundOn]) < strlen($tTargetFormat[$z])) {
                                $tResult[$z] = $tDate[$tFoundOn];
                            } else {
                                $tResult[$z] = $tDate[$tFoundOn];
                            }
                        } else {
                            $tResult[$z] = $tDate[0];
                        }
                    }
                    $tempResult = implode("-", $tResult);
                    $tempVariable = $pInputName . '_' . $j;
                    $_POST[$tempVariable] = $tempResult;
                } else {
                    //TIDAK BERLAKU UNTUK NILAI KOSONG
                }
            }
        } else {
            if (!empty($_POST[$pInputName])) {
                $tDate = explode($mSeparator, $_POST[$pInputName]);

                //LAKUKAN KONVERSI, Timpa variabel global yang asli
                //PENCOCOKAN URUTAN (SELALU 3 urutan yaitu D, M dan Y)
                for ($z = 0; $z < 3; $z++) {
                    if ($tTargetFormat[$z] != $tResFormat[0]) {
                        $tTarget = substr($tTargetFormat[$z], 0, 1);

                        $x = 0;
                        $tFoundOn = 0;
                        while ($tResFormat[$x]) {
                            $temp = substr($tResFormat[$x], 0, 1);
                            //echo $tResFormat[$x].'|: '.$temp.' | '.$tTarget.'<br>';  
                            if ($tTarget == $temp) {
                                $tFoundOn = $x;
                                break;
                            }
                            $x++;
                        }
                        //echo '<br>'.$z.' |: FOUND: '.$tFoundOn.' | '.$tTargetFormat[$z].' | '.$tResFormat[0].'<br>';

                        if (strlen($tResFormat[$tFoundOn]) < strlen($tTargetFormat[$z])) {
                            $tResult[$z] = $tDate[$tFoundOn];
                        } else {
                            $tResult[$z] = $tDate[$tFoundOn];
                        }
                    } else {
                        $tResult[$z] = $tDate[0];
                    }
                }
                $tempResult = implode("-", $tResult);
                $_POST[$pInputName] = $tempResult;
            } else {
                //TIDAK BERLAKU UNTUK NILAI KOSONG
            }
        }

        if (CORE_DEVELOPING_MODE) {
            echo "<div class=\"Debug\"><h3>ConvertDateToDBFormat</h3>" .
            "VAR: " . $tempVariable . "\n" .
            $pCurrentInput . " => " . $_POST[$pInputName] . "</div>";
        }
    }

    public function CheckValiditasPattern($pPattern, $pData) {
        // Desc. Fungsi : Fungsi untuk melakukan validitas data (lebih dari satu) melalui fungsi Checksum.
        // 
        // PARAMATER 1
        // Nama var : $pPattern 
        // Tipe var : String dengan separator "|"
        // Desc.    : Pattern yang digunakan untuk checksum data yang telah dideklarasikan terlebih dahulu
        // 
        // PARAMATER 2
        // Nama var : $pData 
        // Tipe var : Array of String (1D). 
        //            Key Value adalah nama field dari tabel database
        // Desc.    : 

        $tPattern = explode("|", $pPattern);

        $tStr = APPLICATION_VALIDITAS_HEAD;
        foreach ($tPattern as $tRes) {
            //CHECK: String Langsung Disertakan / NUMERIC artinya index value
            if (is_numeric($tRes))
                $tStr.= $pData[$tRes];
            else if (is_string($tRes))
                $tStr.= $tRes;
        }
        $tStr.= APPLICATION_VALIDITAS_TAIL;

        $tValiditasValue = crc32($tStr);
        $tValiditasValue = sprintf("%u", $tValiditas);

        if ($this->mValiditasDebug)
            echo $tStr;

        return $tValiditasValue;
    }

    public function CheckAuthenticationAllow($tAuthLevel) {
        $tIsAllow = false;

        if ($tAuthLevel == true) {
            $tIsAllow = true;
        } else {
            if ($tLevelLogin == 1) {
                $tIsAllow = true;
            } else {
                //echo 'BUTTON AUTH: TRUE <br>';
                $tAuth = explode("|", $tAuthLevel);

                $i = 0;
                while ($tAuth[$i] != "") {
                    if ($tAuth[$i] == $_SESSION['user']['level'])
                        $tIsAllow = true;
                    $i++;
                }
            }
        }

        return $tIsAllow;
    }

    /* ========================================================================== */
    /* ========================================================================== */
    /*                A R R A Y   M A N I P U L A T I O N                         */
    /* ========================================================================== */

    public function CalibrateArray(&$tArray) {
        $i = 0;
        $tTemp = array();
        foreach ($tArray as $tRes) {
            $tTemp[$i] = $tRes;
            $i++;
        }

        //PERIKSA apakah ada hasil dari calibrating, jika ya eksekusi
        if ($tTemp) {
            //RESET DATA FIELD
            $tArray = $tTemp;
        }
    }

    public function GetArrayIndexOf($tArray) {
        $tCount = count($tArray);
        (!empty($tCount)) ? $tIndex = $tCount : $tIndex = 0;

        return $tIndex;
    }

    public function GetArrayKeyValue($pRowData, $pSearchValue) {
        $tKeyValue = false;
        $tNumericKeyValue= null;

        $tKeys = array_keys($pRowData, $pSearchValue);

        if (is_array($tKeys)) {
            foreach ($tKeys as $tResKeyValue) {
                if (!is_numeric($tResKeyValue))
                    $tKeyValue= $tResKeyValue;
                else
                    $tNumericKeyValue= $tResKeyValue;
            }
        }
        
        if($tKeyValue === false)
            $tKeyValue= $tNumericKeyValue;
        return $tKeyValue;
    }

}

?>
